package com.javashizhan.demo.restclient.client.core;


import java.util.Objects;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.javashizhan.demo.restclient.client.proxy.RestClientProxyFactory;

class RestClientFactoryBean implements FactoryBean<Object>, InitializingBean, ApplicationContextAware {

	private Class<?> type;

	private ApplicationContext applicationContext;


	@Override
	public void afterPropertiesSet() throws Exception {
	}

	@Override
	public Object getObject() throws Exception {
		return RestClientProxyFactory.getProxy(type);
	}

	@Override
	public Class<?> getObjectType() {
		return this.type;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	public Class<?> getType() {
		return this.type;
	}

	public void setType(Class<?> type) {
		this.type = type;
	}



	public ApplicationContext getApplicationContext() {
		return this.applicationContext;
	}

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		this.applicationContext = context;
	}



	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}
		RestClientFactoryBean that = (RestClientFactoryBean) o;
		return Objects.equals(this.applicationContext, that.applicationContext) 
				&& Objects.equals(this.type, that.type);
	}

	@Override
	public int hashCode() {
		return Objects.hash(this.applicationContext, this.type);
	}

	@Override
	public String toString() {
		return new StringBuilder("RestClientFactoryBean{").append("type='").append(this.type).append("'}").toString();
	}

}

